/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame.TaiKhoan;

import dao.NhanVienDAO;
import dao.PhanQuyenDAO;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import myclass.MyAdminFrame;
import pojo.Nhanvien;
import pojo.Phanquyen;

/**
 *
 * @author Quan
 */
public class PhanQuyen extends MyAdminFrame {
    
    public static String TENKHUVUC = "Phân Quyền";
    private DefaultTableModel nhanvienmodel;
    private DefaultTableModel quyenmodel;

    /**
     * Creates new form PhanQuyen
     */
    public PhanQuyen(Nhanvien nv) {
        super(nv);
        initComponents();
        
        nhanvienmodel = new DefaultTableModel();
        nhanvienmodel.addColumn("Tên Đăng Nhập");
        nhanvienmodel.addColumn("Họ Tên");
        nhanvienmodel.addColumn("Tổng Số Quyên");
        jtable_nv.setModel(nhanvienmodel);
        LoadNhanVien();
        
        quyenmodel = new DefaultTableModel();
        jtable_quyen.setModel(quyenmodel);
        quyenmodel.addColumn("STT");
        quyenmodel.addColumn("Khu Vực");
        quyenmodel.addColumn("Quyền");
        removeSelection();
    }
    
    private void removeSelection() {
        jcbb_quyen.setSelectedIndex(-1);
        jcbb_khuvuc.setSelectedIndex(-1);
        jtf_id.setText(null);
    }
    
    private void LoadNhanVien() {
        nhanvienmodel.getDataVector().removeAllElements();
        List<Nhanvien> nhanviens = null;
        NhanVienDAO nvdao = new NhanVienDAO();
        try {
            nhanviens = nvdao.LayDanhSach();
        } catch (Exception ex) {
            Logger.getLogger(PhanQuyen.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (nhanviens != null) {
            for (int i = 0; i < nhanviens.size(); i++) {
                Nhanvien nhanvien = nhanviens.get(i);
                nhanvienmodel.addRow(new Object[]{
                            nhanvien.getTendangnhap(),
                            nhanvien,
                            nhanvien.getPhanquyens().size()
                        });
            }
        }
    }
    
    private void LoadQuyen() {
        quyenmodel.getDataVector().removeAllElements();
        quyenmodel.fireTableDataChanged();
        
        int i = jtable_nv.getSelectedRow();
        if (i != -1) {
            Nhanvien nhanvien = (Nhanvien) nhanvienmodel.getValueAt(i, 1);
            PhanQuyenDAO phanQuyenDAO = new PhanQuyenDAO();
            List phanQuyens = null;
            try {
                phanQuyens = phanQuyenDAO.LayQuyenCuaUser(nhanvien);
            } catch (Exception ex) {
                Logger.getLogger(PhanQuyen.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int j = 0; j < phanQuyens.size(); j++) {
                Phanquyen phanQuyen = (Phanquyen) phanQuyens.get(j);
                quyenmodel.addRow(new Object[]{
                            (j + 1),
                            phanQuyen,
                            phanQuyen.getQuyen()
                        });
            }
            removeSelection();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtable_nv = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtable_quyen = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jcbb_quyen = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jcbb_khuvuc = new javax.swing.JComboBox();
        jtf_id = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Phân Quyền");

        jtable_nv.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtable_nv.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jtable_nv.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtable_nvMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtable_nv);

        jtable_quyen.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtable_quyen.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtable_quyenMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jtable_quyen);

        jLabel1.setText("Quyền");

        jLabel2.setText("Nhân viên");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Khung Điều Chỉnh"));

        jLabel3.setText("ID");

        jLabel4.setText("Quyền");

        jcbb_quyen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Xem", "Chỉnh sửa", "Disable", "Thêm / Xóa" }));

        jLabel5.setText("Khu Vực");

        jcbb_khuvuc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kho", "Bán Hàng", "Báo Cáo", "Khách Hàng", "Nhân Viên", "Phân Quyền" }));

        jtf_id.setEditable(false);

        jButton1.setText("Thêm mới");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Sửa");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Xóa");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Đóng");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbb_quyen, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jcbb_khuvuc, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jtf_id)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(44, 44, 44)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtf_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jcbb_quyen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jcbb_khuvuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed
    
    private void jtable_nvMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtable_nvMouseClicked
        LoadQuyen();
    }//GEN-LAST:event_jtable_nvMouseClicked
    
    private void jtable_quyenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtable_quyenMouseClicked
        int i = jtable_quyen.getSelectedRow();
        if (i != -1) {
            Phanquyen ph = (Phanquyen) jtable_quyen.getValueAt(i, 1);
            jtf_id.setText(String.valueOf(ph.getId()));
            Phanquyen phanquyen = (Phanquyen) jtable_quyen.getValueAt(i, 1);
            jcbb_khuvuc.setSelectedItem(ph.getKhuvuc());
            jcbb_quyen.setSelectedItem(ph.getQuyen());
        }
    }//GEN-LAST:event_jtable_quyenMouseClicked
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        long id = 0;
        try {
            id = Long.valueOf(jtf_id.getText());
        } catch (Exception e) {
            return;
        }
        
        PhanQuyenDAO pqdao = new PhanQuyenDAO();
        Phanquyen phanquyen = null;
        try {
            phanquyen = (Phanquyen) pqdao.Lay(id);
        } catch (Exception ex) {
            Logger.getLogger(PhanQuyen.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        phanquyen.setKhuvuc((String) jcbb_khuvuc.getSelectedItem());
        phanquyen.setQuyen((String) jcbb_quyen.getSelectedItem());
        
        try {
            pqdao.Sua(phanquyen);
            LoadQuyen();
            JOptionPane.showMessageDialog(rootPane, "Sửa thành công", "Thông Báo", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception ex) {
            Logger.getLogger(PhanQuyen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        long id = 0;
        try {
            id = Long.valueOf(jtf_id.getText());
        } catch (Exception e) {
            return;
        }
        
        PhanQuyenDAO pqdao = new PhanQuyenDAO();
        Phanquyen phanquyen = null;
        try {
            phanquyen = (Phanquyen) pqdao.Lay(id);
            pqdao.Xoa(phanquyen);
            LoadQuyen();
            LoadNhanVien();
            JOptionPane.showMessageDialog(rootPane, "Xóa thành công", "Thông Báo", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception ex) {
            Logger.getLogger(PhanQuyen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton3ActionPerformed
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        int i = jtable_nv.getSelectedColumn();
        Nhanvien nhanvien = (Nhanvien) nhanvienmodel.getValueAt(i, 1);
        
        PhanQuyenDAO pqdao = new PhanQuyenDAO();
        Phanquyen phanquyen = new Phanquyen();
        phanquyen.setNhanvien(nhanvien);
        phanquyen.setKhuvuc((String) jcbb_khuvuc.getSelectedItem());
        phanquyen.setQuyen((String) jcbb_quyen.getSelectedItem());
        try {
            pqdao.Them(phanquyen);
            LoadQuyen();
            LoadNhanVien();
            JOptionPane.showMessageDialog(rootPane, "Thêm thành công", "Thông Báo", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception ex) {
            Logger.getLogger(PhanQuyen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PhanQuyen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PhanQuyen(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JComboBox jcbb_khuvuc;
    private javax.swing.JComboBox jcbb_quyen;
    private javax.swing.JTable jtable_nv;
    private javax.swing.JTable jtable_quyen;
    private javax.swing.JTextField jtf_id;
    // End of variables declaration//GEN-END:variables
}
